package com.atung.parking.controller;

import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusParkingIot;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.ParkingIotApprovalState;
import com.atung.parking.mapstruct.ParkingIotConverter;
import com.atung.parking.request.ApprovalParkingIotRequest;
import com.atung.parking.request.query.AppQueryParkingIotRequest;
import com.atung.parking.request.query.QueryParkingIotRequest;
import com.atung.parking.request.save.SaveOrUpdateParkingIotRequest;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.HardwareVendorResponse;
import com.atung.parking.response.page.ParkingIotResponse;
import com.atung.parking.response.page.SimpleParkingIotResponse;
import com.atung.parking.service.ParkingIotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@Api(tags = "停车场管理")
@RequestMapping("/api/parking")
public class ParkingIotController {
    @Resource
    private ParkingIotService parkingIotService;
    @Resource
    private ParkingIotConverter parkingIotConverter;

    @PostMapping("/save")
    @ApiOperation("新增停车场")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult saveParkingIot(@RequestBody @Valid SaveOrUpdateParkingIotRequest request) {
        return parkingIotService.saveParkingIotService(request);
    }

    @GetMapping("/query")
    @ApiOperation("获取停车场列表")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult<PageResponse<ParkingIotResponse>> queryParkingIotList(QueryParkingIotRequest request) {
        PageInfoResponse<BusParkingIot> pageInfoResponse = parkingIotService.queryParkingIotListService(request);
        return ApiResult.ok(pageInfoResponse.getData().stream().map(parkingIotConverter::toResponse)
                .collect(Collectors.toList()), request.getPageNum(), request.getPageSize(), pageInfoResponse.getTotal());
    }

    @GetMapping("/query/simple")
    @ApiOperation("获取停车场列表(无分页下拉列表)")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult<List<SimpleParkingIotResponse>> querySimpleParkingIotList() {
        List<BusParkingIot> busParkingIotList = parkingIotService.querySimpleParkingIotListService();
        return ApiResult.ok(busParkingIotList.stream().map(parkingIotConverter::toSimpleResponse).collect(Collectors.toList()));
    }

    @GetMapping("/query/hardwareVendor")
    @ApiOperation("获取厂商数据(无分页下拉列表)")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult<List<HardwareVendorResponse>> queryParkingIotHardwareVendorList() {
        List<HardwareVendorResponse> hardwareVendorList = parkingIotService.queryParkingIotHardwareVendorList();
        return ApiResult.ok(hardwareVendorList);
    }

    @PostMapping("/approval")
    @ApiOperation("审核停车场")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult approvalParkingIot(@RequestBody @Valid ApprovalParkingIotRequest request) {
        return parkingIotService.approvalParkingIotService(request);
    }

    @GetMapping("/query/approval/{id}")
    @ApiOperation("获取审核车场审核记录")
    @PermissionValid(roleType = {AdminType.SUPERADMIN,AdminType.ADMIN})
    public ApiResult<List<ApprovalRecordResponse>> queryApprovalParkingIotList(@PathVariable("id") Long id) {
        return ApiResult.ok(parkingIotService.queryApprovalParkingIotList(id));
    }

    @GetMapping("/approval/state")
    @ApiOperation("获取审核状态下拉数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult<List<Map<String, String>>> getPackageTypeList() {
        List<Map<String, String>> packageTypeList = new ArrayList<>();
        for (ParkingIotApprovalState packageType : ParkingIotApprovalState.values()) {
            Map<String, String> packageTypeMap = new HashMap<>();
            packageTypeMap.put("name", packageType.getDescription());
            packageTypeMap.put("value", packageType.name());
            packageTypeList.add(packageTypeMap);
        }
        return ApiResult.ok(packageTypeList);
    }

    @GetMapping("/export")
    @ApiOperation("导出车场列表")
//    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public void exportParkingIotFranchise(QueryParkingIotRequest request, HttpServletResponse httpServletResponse) {
        parkingIotService.exportParkingIotService(request, httpServletResponse);
    }
    @GetMapping("/appDetails/{id}")
    @ApiOperation("小程序获取(预订车位详情)")
    public ApiResult appParkingIotDetails(@PathVariable("id")Long id){
        return parkingIotService.appParkingIotDetailsService(id);
    }

}
